package com.rk.msm.service.impl;
import com.apistd.uni.Uni;
import com.apistd.uni.UniException;
import com.apistd.uni.UniResponse;
import com.apistd.uni.sms.UniMessage;
import com.apistd.uni.sms.UniSMS;
import com.rk.common.exception.YyghException;
import com.rk.common.result.ResultCodeEnum;
import com.rk.hosp.vo.msm.MsmVo;
import com.rk.msm.service.MsmService;
import com.rk.msm.util.SendMailUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailSendException;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author :Rk.
 * @date : 2022/11/22
 */
@Slf4j
@Service
public class MsmServiceImpl implements MsmService {

    @Autowired
    private SendMailUtil sendMailUtil;

    private  final String subject="预约挂号系统登录";

    @Value("${unisms.accessKeyId}")
    private String accessKeyId;


    @Override
    public boolean sendEmail(String email, String code) {
        log.info("给{}发送验证码:{}",email,code);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        String emailTemplate = "loginTemplate";
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("code", code);
        dataMap.put("createTime", sdf.format(new Date()));
        try {
            sendMailUtil.sendTemplateMail(email, subject, emailTemplate, dataMap);
            log.info("发送完成");
            return true;
        } catch (MailSendException e){
            throw new YyghException(ResultCodeEnum.Email_ERROR);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean sendMessage(String phone, String code) {
        log.info("给{}发送验证码:{}",phone,code);

        // 初始化 // 若使用简易验签模式仅传入第一个参数即可
        Uni.init(accessKeyId);

        // 设置自定义参数 (变量短信)
        Map<String, String> templateData = new HashMap<String, String>();
        templateData.put("code", code);

        // 构建信息
        UniMessage message = UniSMS.buildMessage()
                .setTo(phone)
                .setSignature("罗林测试")
                .setTemplateId("pub_verif_login_ttl")
                .setTemplateData(templateData);

        // 发送短信
        try {
            UniResponse res = message.send();
            log.info("发送完成!");
            return true;
        } catch (UniException e) {
            e.printStackTrace();
            log.info("发送失败!");
            return false;
        }

    }

    @Override
    public boolean send(MsmVo msmVo) throws Exception {
        String templateCode = msmVo.getTemplateCode();
        String subject="";
        //如果是预约排班的邮件
        if("orderTemplate".equals(templateCode)){
             subject="预约成功,请查收!";
        }else if("cancelTemplate".equals(templateCode)){//如果是取消预约的邮件
            subject="取消预约!";
        }else if("TipsPatientTemplate".equals(templateCode)){
            subject="预约提醒!";
        }else if("TipsPaymentTemplate".equals(templateCode)){
            subject="缴费提醒!";
        }
        Map<String, Object> param = msmVo.getParam();
        String email = msmVo.getPhone();

        //发送邮件
        sendMailUtil.sendTemplateMail(email, subject, templateCode, param);

        return true;
    }
}
